package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import controller.Sistema;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ModificacionMaterial extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel labelStock;
	private JLabel labelPuntoPedido;
	private JButton botonCancelar;
	private JComboBox<MaterialView> listaMats;
	private JLabel labelMaterial;
	private JComboBox<ProveedorView> listaProv;
	private JTextField campoCosto;
	private JTextField campoPtoPedido;
	private JTextField campoStock;
	private JButton botonAceptar;
	private JLabel labelProveedor;
	private JLabel labelCosto;
	private JLabel labelTitulo;
	private JOptionPane MensajeConfirmacion;	
	
	public ModificacionMaterial() {
		super();
		initGUI();
	}
	
	class MultiColumnCellRendererProveedorView extends JPanel implements ListCellRenderer<ProveedorView>{
		private static final long serialVersionUID = 1L;
		private final JLabel leftLabel = new JLabel();
		  private final JLabel rightLabel;
		  public MultiColumnCellRendererProveedorView(int rightWidth) {
		    super(new BorderLayout());
		    this.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));

		    leftLabel.setOpaque(false);
		    leftLabel.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));

		    final Dimension dim = new Dimension(rightWidth, 0);
		    rightLabel = new JLabel() {
				private static final long serialVersionUID = 1L;

			@Override public Dimension getPreferredSize() {
		        return dim;
		      }
		    };
		    rightLabel.setOpaque(false);
		    rightLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,2));
		    rightLabel.setForeground(Color.GRAY);
		    rightLabel.setHorizontalAlignment(SwingConstants.RIGHT);

		    this.add(leftLabel);
		    this.add(rightLabel, BorderLayout.EAST);
		  }
		  @Override public Component getListCellRendererComponent(JList<? extends ProveedorView> list, 
				  ProveedorView value, int index, boolean isSelected, boolean cellHasFocus) {
			  
		    leftLabel.setText(String.valueOf(value.getCuit()));
		    rightLabel.setText(value.getRazonSocial());		    

		    leftLabel.setFont(list.getFont());
		    rightLabel.setFont(list.getFont());

		    if(index<0) {
		      leftLabel.setForeground(list.getForeground());
		      this.setOpaque(false);
		    }else{
		      leftLabel.setForeground(
		          isSelected?list.getSelectionForeground():list.getForeground());
		      this.setBackground(
		          isSelected?list.getSelectionBackground():list.getBackground());
		      this.setOpaque(true);
		    }
		    return this;
		  }
		  
		  @Override public Dimension getPreferredSize() {
		    Dimension d = super.getPreferredSize();
		    return new Dimension(0, d.height);
		  }		  
	}
	
	class MultiColumnCellRendererMaterialView extends JPanel implements ListCellRenderer<MaterialView>{
		private static final long serialVersionUID = 1L;
		private final JLabel leftLabel = new JLabel();
		  private final JLabel rightLabel;
		  public MultiColumnCellRendererMaterialView(int rightWidth) {
		    super(new BorderLayout());
		    this.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));

		    leftLabel.setOpaque(false);
		    leftLabel.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));

		    final Dimension dim = new Dimension(rightWidth, 0);
		    rightLabel = new JLabel() {
				private static final long serialVersionUID = 1L;

			@Override public Dimension getPreferredSize() {
		        return dim;
		      }
		    };
		    rightLabel.setOpaque(false);
		    rightLabel.setBorder(BorderFactory.createEmptyBorder(0,2,0,2));
		    rightLabel.setForeground(Color.GRAY);
		    rightLabel.setHorizontalAlignment(SwingConstants.RIGHT);

		    this.add(leftLabel);
		    this.add(rightLabel, BorderLayout.EAST);
		  }
		  @Override public Component getListCellRendererComponent(JList<? extends MaterialView> list, 
				  MaterialView value, int index, boolean isSelected, boolean cellHasFocus) {
			  
		    leftLabel.setText(String.valueOf(value.getCodigo()));
		    rightLabel.setText(value.getNombre());

		    leftLabel.setFont(list.getFont());
		    rightLabel.setFont(list.getFont());

		    if(index<0) {
		      leftLabel.setForeground(list.getForeground());
		      this.setOpaque(false);
		    }else{
		      leftLabel.setForeground(
		          isSelected?list.getSelectionForeground():list.getForeground());
		      this.setBackground(
		          isSelected?list.getSelectionBackground():list.getBackground());
		      this.setOpaque(true);
		    }
		    return this;
		  }
		  
		  @Override public Dimension getPreferredSize() {
		    Dimension d = super.getPreferredSize();
		    return new Dimension(0, d.height);
		  }		  
	}
	
	private void initGUI() {
		try {
			this.setSize(496, 340);
			setLocationRelativeTo(null);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				labelTitulo = new JLabel();
				this.add(labelTitulo);
				labelTitulo.setText("Modificacion de Material");
				labelTitulo.setBounds(12, 12, 233, 27);
				labelTitulo.setFont(new java.awt.Font("Segoe UI",0,16));
			}
			{
				labelStock = new JLabel();
				this.add(labelStock);
				labelStock.setText("Stock");
				labelStock.setBounds(12, 107, 178, 16);
			}
			{
				labelPuntoPedido = new JLabel();
				this.add(labelPuntoPedido);
				labelPuntoPedido.setText("Punto de pedido");
				labelPuntoPedido.setBounds(12, 135, 178, 16);
			}
			{
				labelCosto = new JLabel();
				this.add(labelCosto);
				labelCosto.setText("Costo $");
				labelCosto.setBounds(12, 163, 135, 16);
			}
			{
				labelProveedor = new JLabel();
				this.add(labelProveedor);
				labelProveedor.setText("Proveedor");
				labelProveedor.setBounds(12, 191, 157, 16);
			}
			{
				botonAceptar = new JButton();
				this.add(botonAceptar);
				botonAceptar.setText("Aceptar");
				botonAceptar.setBounds(270, 233, 109, 37);
				botonAceptar.setFont(new java.awt.Font("Segoe UI",0,16));
				botonAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						try {
							MaterialView m = (MaterialView) listaMats.getSelectedItem();
							ProveedorView p = (ProveedorView) listaProv.getSelectedItem();
							Sistema.getInstance().actualizarMaterial(
									m.getCodigo(),
									m.getNombre(),
									Integer.valueOf(campoStock.getText()),
									Integer.valueOf(campoPtoPedido.getText()),
									Float.valueOf(campoCosto.getText()),
									p.getCuit()
							);
							JOptionPane.showMessageDialog(MensajeConfirmacion, "El Material se actualizo correctamente.");
							setVisible(false);
						}
						catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
			{
				campoStock = new JTextField();
				this.add(campoStock);
				campoStock.setBounds(208, 104, 244, 23);
			}
			{
				campoPtoPedido = new JTextField();
				this.add(campoPtoPedido);
				campoPtoPedido.setBounds(208, 132, 244, 23);
			}
			{
				campoCosto = new JTextField();
				this.add(campoCosto);
				campoCosto.setBounds(208, 160, 244, 23);
			}
			{
				Vector<ProveedorView> proveedores = Sistema.getInstance().obtenerVistaProveedores();
				ListCellRenderer<ProveedorView> renderer = new MultiColumnCellRendererProveedorView(94);
				ComboBoxModel<ProveedorView> listaProvModel = new DefaultComboBoxModel<ProveedorView>(proveedores);
				listaProv = new JComboBox<ProveedorView>(listaProvModel);
				listaProv.setRenderer(renderer);
				this.add(listaProv);
				listaProv.setModel(listaProvModel);
				listaProv.setBounds(208, 188, 244, 23);
			}
			{
				labelMaterial = new JLabel();
				this.add(labelMaterial);
				labelMaterial.setText("Seleccione Material...");
				labelMaterial.setBounds(12, 51, 125, 16);
			}
			{	
				Vector<MaterialView> materiales = Sistema.getInstance().obtenerVistaMateriales();
				ListCellRenderer<MaterialView> renderer = new MultiColumnCellRendererMaterialView(94);
				ComboBoxModel<MaterialView> listaMatsModel = new DefaultComboBoxModel<MaterialView>(materiales);
				listaMats = new JComboBox<MaterialView>(listaMatsModel);
				this.add(listaMats);
				listaMats.setRenderer(renderer);
				listaMats.setModel(listaMatsModel);
				listaMats.setBounds(208, 48, 244, 23);
			}
			{
				botonCancelar = new JButton();
				getContentPane().add(botonCancelar);
				botonCancelar.setFont(new java.awt.Font("Segoe UI",0,16));
				botonCancelar.setBounds(114, 233, 109, 37);
				botonCancelar.setText("Cancelar");
				botonCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						close();
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void close(){
		this.dispose();
	}
}
